//
//  TableViewCell.m
//  两种Cell间的切换
//
//  Created by 小果 on 16/7/13.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "TableViewCell.h"
#import "TableCellFrame.h"
#import "CellModel.h"

@interface TableViewCell ()
@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel *Tname;
@property (nonatomic, weak) UILabel *detail;
@property (nonatomic, weak) UIView *SepLine;

@end

@implementation TableViewCell
+(instancetype)tableCellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"tableCell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        // 设置Cell中的控件
        [self setupTableCellWithSubViews];
    }
    return self;
}
#pragma mark - 设置Cell中的控件
-(void)setupTableCellWithSubViews{
    
    UIImageView *imgView = [[UIImageView alloc] init];
    self.imgView = imgView;
    [self.contentView addSubview:imgView];
    
    UILabel *tlabel = [[UILabel alloc] init];
    tlabel.font = TnameFont;
    tlabel.textColor = [UIColor redColor];
    self.Tname = tlabel;
    [self.contentView addSubview:tlabel];
    
    UILabel *dLabel = [[UILabel alloc] init];
    dLabel.font = detailFont;
    self.detail = dLabel;
    [self.contentView addSubview:dLabel];
    
    UILabel *bgValue = [[UILabel alloc] init];
    self.bgValue = bgValue;
    bgValue.textColor = [UIColor whiteColor];
    bgValue.font = bgValueFont;
    bgValue.backgroundColor = bgValueBgColor;
    bgValue.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:bgValue];
    
    UIView *sepL = [[UIView alloc] init];
    sepL.backgroundColor = [UIColor redColor];
    self.SepLine = sepL;
    [self.contentView addSubview:sepL];
    
}
#pragma mark - 设置Cell中控件的frame和内容
-(void)setCellFrame:(TableCellFrame *)cellFrame{
    _cellFrame = cellFrame;
    
    [self settingCellFrame];
    
    [self settingCellContentWithValue:cellFrame];
}
#pragma mark - 设置frame
-(void)settingCellFrame{
    self.imgView.frame = self.cellFrame.imgF;
    
    self.Tname.frame = self.cellFrame.nameF;
    
    self.detail.frame = self.cellFrame.detailF;
    
    self.SepLine.frame = self.cellFrame.sepLine;
    
    if (self.cellFrame.cellModel.value){
        self.bgValue.hidden = NO;
        self.bgValue.frame =  self.cellFrame.bgValueF;
    }else{
        self.bgValue.hidden = YES;
    }
}
#pragma mark - 设置Cell中的内容
-(void)settingCellContentWithValue:(TableCellFrame *)cellFrame{
    
    CellModel *model = cellFrame.cellModel;
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"1.gif" withExtension:nil];
    [self.imgView sd_setImageWithURL:url];
    
    self.Tname.text = model.name;
    
    self.detail.text = model.detail;
    
    if (model.value){
        int Num = model.value.intValue;
        if (Num >= 100){
            self.bgValue.text = @"99+";
        }else if (Num == 0){
            self.bgValue.hidden = YES;
        }else{
            self.bgValue.text = model.value;
        }
    }
}
#pragma mark - 设置bgValue的圆角
-(void)layoutSubviews{
    [super layoutSubviews];
    self.bgValue.layer.cornerRadius = self.bgValue.frame.size.width * 0.5;
    self.bgValue.layer.borderColor = [UIColor greenColor].CGColor;
    self.bgValue.layer.borderWidth = 2;
    self.bgValue.layer.masksToBounds = YES;
}
@end
